package com.alpha.service.utils;

import com.alpha.core.entity.AlphaQuery;
import com.alpha.core.http.Response;
import com.alpha.core.utils.BeanUtil;
import com.alpha.core.utils.CollectionUtil;
import com.alpha.core.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * mybatis-plus 辅助类
 *
 * @author chenruwan
 * @since 2022-01-01
 */
@Slf4j
@SuppressWarnings("unused")
public class MyBatisPlusUtils {
    private MyBatisPlusUtils() {
    }

    public static <T> IPage<T> pageOrList(IService<T> service, AlphaQuery query, QueryWrapper<T> wrapper) {
        IPage<T> data;
        if (query.page() == null) {
            data = buildPage(service.list(wrapper));
        } else {
            data = service.page(query.page(), wrapper);
        }
        return data;
    }

    public static <T> Page<T> buildPage(List<T> records) {
        return new Page<T>(1, records.size(), records.size()).setRecords(records);
    }

    public static <T> List<T> selectBatchIds(BaseMapper<T> mapper, Collection<? extends Serializable> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return java.util.Collections.emptyList();
        }
        return mapper.selectBatchIds(ids);
    }

    public static <T> List<T> listByIds(IService<T> service, Collection<? extends Serializable> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return java.util.Collections.emptyList();
        }
        return service.listByIds(ids);
    }

    public static <T> Response<Integer> deleteBatchIds(BaseMapper<T> mapper, List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            Response.success(0);
        }

        int count = mapper.deleteBatchIds(ids);
        if (count == 0) {
            return Response.failure(HttpStatus.BAD_REQUEST.value(), "数据删除失败", count);
        } else if (count < ids.size()) {
            return Response.success(count, "数据删除部分成功");
        }
        return Response.success(count);
    }

    public static <T> Page<T> copyPage(Page<?> src, Class<T> targetType) {
        Page<T> page = copyPageHeader(src);
        return page.setRecords(BeanUtil.copyToList(src.getRecords(), targetType));
    }

    public static <T> Page<T> copyPageHeader(Page<?> src) {
        Page<T> dest = new Page<>(src.getCurrent(), src.getSize(), src.getTotal());

        dest.setOrders(src.orders());
        dest.setOptimizeCountSql(src.optimizeCountSql());
        dest.setSearchCount(src.searchCount());
        dest.setCountId(src.countId());
        dest.setMaxLimit(src.maxLimit());
        dest.setPages(src.getPages());

        return dest;
    }

    public static <T> String buildInSql(String table, String selected, String column, T value) {
        return StringUtil.format("SELECT DISTINCT({}) FROM {} WHERE {} = '{}'",
                selected, table, column, value);
    }
}
